package presentation.web;

import presentation.UIMember ;
import presentation.UIUser ;

/**
 * The Class LoginPage.
 */
public class LogoutPage extends MainPage
{

	private boolean error ;

	/**
	 * Instantiates a new logout page.
	 *
	 */
	public LogoutPage()
	{
		super();
		try
		{
			if (!this.isUserLoggedIn())
			{
				this.navigateToErrorPage("you are not logged in", null) ;
				this.error = true ;
				return ;
			}
			this.error = false ;
			this.getForumLogic().exitUser(this.getUser().getId()) ;
			this.getAllSessionData(false).remove("user") ;
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null) ;
		}
	}

	/**
	 * @see presentation.web.MainPage#onLoad()
	 */
	@Override
	protected void onLoad()
	{
		super.onLoad() ;
		if (this.error)
		{
			return ;
		}

		try
		{
			this.navigateToPage(MainPage.HOME_PAGE) ;
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null);
		}
	}

	private boolean isUserLoggedIn()
	{
		UIUser user = null;
		try
		{
			user = this.getUser() ;
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null);
		}
		return user instanceof UIMember ;
	}
}

